<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <button id="button1">点击按钮 1</button>
    <button id="button2">点击按钮 2</button>
    <button id="button3">点击按钮 3</button>

    <script>
        // 非js中优化方法，而是函数表示
        let button1 = document.getElementById('button1'),
            button2 = document.getElementById('button2'),
            button3 = document.getElementById('button3');

        const setCommand = function (button, command) {
            button.onclick = function () {
                command.execute();
            }
        }

        var MenuBar = {
            refresh: function () {
                console.log('刷新菜单目录');
            }
        };
        var SubMenu = {
            add: function () {
                console.log('增加子菜单');
            },
            del: function () {
                console.log('删除子菜单');
            }
        };

        var RefreshMenuBarCommand = function (receiver) {
            this.receiver = receiver;
        };
        RefreshMenuBarCommand.prototype.execute = function () {
            this.receiver.refresh();
        };
        var AddSubMenuCommand = function (receiver) {
            this.receiver = receiver;
        };

        AddSubMenuCommand.prototype.execute = function () {
            this.receiver.add();
        };
        var DelSubMenuCommand = function (receiver) {
            this.receiver = receiver;
        };
        DelSubMenuCommand.prototype.execute = function () {
            console.log('删除子菜单');
        };

        var refreshMenuBarCommand = new RefreshMenuBarCommand(MenuBar);
        var addSubMenuCommand = new AddSubMenuCommand(SubMenu);
        var delSubMenuCommand = new DelSubMenuCommand(SubMenu);
        setCommand(button1, refreshMenuBarCommand);
        setCommand(button2, addSubMenuCommand);
        setCommand(button3, delSubMenuCommand);
    </script>
</body>

</html>